package org.scboy.common.filter;

import java.io.IOException;

import org.scboy.common.cache.LoginContext;
import org.scboy.common.request.CacheRequest;
import org.scboy.common.request.CacheResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.filter.OncePerRequestFilter;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class HttpBodyRecorderFilter  extends OncePerRequestFilter {

	Log log=LogFactory.get();
	

	@Autowired
	LoginContext loginContext;
	
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    	
        CacheRequest requestToUse = new CacheRequest(request);
        
        /**
         * 把登录用户信息放入属性中
         */
        if(loginContext!=null) {
            requestToUse.setAttribute("LOGIN_USER", loginContext.getLogin());
        }
        
        CacheResponse responseToUse  = new CacheResponse(response);
        filterChain.doFilter(requestToUse, responseToUse);
        responseToUse.copyBodyToResponse();

    }

}
